package org.appfuse.webapp.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.appfuse.Constants;
import org.appfuse.model.BasicInfo;
import org.appfuse.model.Cgjxx;
import org.appfuse.model.DutyExperience;
import org.appfuse.model.User;
import org.appfuse.service.BasicInfoManager;
import org.appfuse.service.CgjxxManager;
import org.appfuse.service.DutyExperienceManager;
import org.appfuse.service.UserManager;
import org.appfuse.webapp.form.CgjxxForm;
import org.appfuse.webapp.form.CgjxxForm;

/**
 * Action class to handle CRUD on a Menu object
 * 
 * @struts.action name="criteriaForm" path="/criterias" scope="request"
 *                validate="false" parameter="method" input="mainMenu"
 * @struts.action name="criteriaForm" path="/editMenu" scope="request"
 *                validate="false" parameter="method" input="list"
 * @struts.action name="criteriaForm" path="/saveMenu" scope="request"
 *                validate="true" parameter="method" input="edit"
 * @struts.action-set-property property="cancellable" value="true"
 * @struts.action-forward name="edit" path="/WEB-INF/pages/criteriaForm.jsp"
 * @struts.action-forward name="list" path="/WEB-INF/pages/criteriaList.jsp"
 * @struts.action-forward name="search" path="/criterias.html" redirect="true"
 */
public final class CgjxxAction extends BaseAction {

	public ActionForward cancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("list");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'delete' method");
		}
		CgjxxForm form = (CgjxxForm) actionForm;

		User currentUser = getCurrentUser(request);

		CgjxxManager mgr = (CgjxxManager) getBean(Constants.CGJXX_MANAGER);
		Integer selectedId = form.getSelectedId();
		String a00 = currentUser.getRecordId();
		BasicInfoManager basicInfoMgr = (BasicInfoManager) getBean(Constants.BASICINFO_MANAGER);
		BasicInfo basicInfo = (BasicInfo) basicInfoMgr.findById(a00);

		Cgjxx cgjxx = (Cgjxx) convert(form);
		cgjxx.setId(selectedId);

		try {
			cgjxx.setBasicInfo(basicInfo);
			mgr.delete(cgjxx);
			HttpSession session = request.getSession();
			List list = (List) session.getAttribute(Constants.CGJXX_LIST);
			list.remove(cgjxx);

		} catch (Exception e) {

		}

		return mapping.findForward("list");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'edit' method");
		}

		CgjxxForm form = (CgjxxForm) actionForm;

		User currentUser = getCurrentUser(request);

		CgjxxManager mgr = (CgjxxManager) getBean(Constants.CGJXX_MANAGER);
		Integer selectedId = form.getSelectedId();
		String a00 = currentUser.getRecordId();
		Cgjxx theObject = mgr.findById(a00, selectedId);

		if (theObject != null) {
			form = (CgjxxForm) convert(theObject);
			updateFormBean(mapping, request, form);
		}

		return mapping.findForward("edit");
	}

	public ActionForward save(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'save' method");
		}

		ActionMessages errors = actionForm.validate(mapping, request);

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			return mapping.findForward("add");
		}

		CgjxxManager mgr = (CgjxxManager) getBean(Constants.CGJXX_MANAGER);
		User user = getCurrentUser(request);
		String a00 = user.getRecordId();

		// Extract attributes and parameters we will need
		ActionMessages messages = new ActionMessages();
		CgjxxForm form = (CgjxxForm) actionForm;
		Cgjxx theObject = (Cgjxx) convert(form);

		BasicInfoManager basicInfoMgr = (BasicInfoManager) getBean(Constants.BASICINFO_MANAGER);
		BasicInfo basicInfo = (BasicInfo) basicInfoMgr.findById(a00);

		HttpSession session = request.getSession();
		List list = (List) session.getAttribute(Constants.CGJXX_LIST);
		try {
			// if id is not null then udpate the record else add it.
			if (form.getId() != null) {
				theObject.setBasicInfo(basicInfo);
				mgr.update(theObject);
				int index = list.indexOf(theObject);
				list.set(index, theObject);
			} else {
				theObject.setId(mgr.newId(a00));
				basicInfo.getCgjxxs().add(theObject);
				theObject.setBasicInfo(basicInfo);
				// mgr.save(cgjxx);
				basicInfoMgr.update(basicInfo);
				form.setId(theObject.getId());
				list.add(theObject);
			}
			theObject.setGb((String) Constants.GB2260_MAP.get(theObject
					.getGb()));
			theObject.setCgmd((String) Constants.GB10301_MAP
					.get(theObject.getCgmd()));
			theObject.setCjsf((String) Constants.ZB33_MAP.get(theObject
					.getCjsf()));
			theObject.setCgjfly((String) Constants.ZB32_MAP.get(theObject
					.getCgjfly()));
			theObject.setPcdw((String) Constants.G099_MAP.get(theObject
					.getPcdw()));

		} catch (Exception e) {
			e.printStackTrace();
		}

		// add success messages
		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				"cgjxx.saved"));
		// save messages in session to survive a redirect
		saveMessages(request.getSession(), messages);

		return mapping.findForward("list");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'save' method");
		}

		return mapping.findForward("edit");
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'list' method");
		}

		User user = getCurrentUser(request);
		HttpSession session = request.getSession();

		String a00 = user.getRecordId();
		if (!StringUtils.isEmpty(a00)) {
			CgjxxManager mgr = (CgjxxManager) getBean(Constants.CGJXX_MANAGER);
			List list = mgr.findByA00(a00);
			if (list == null) {
				list = new ArrayList(0);
			} else {

				Iterator it = list.iterator();
				while (it.hasNext()) {
					Cgjxx theObject = (Cgjxx) it.next();
					theObject.setGb((String) Constants.GB2260_MAP.get(theObject
							.getGb()));
					theObject.setCgmd((String) Constants.GB10301_MAP
							.get(theObject.getCgmd()));
					theObject.setCjsf((String) Constants.ZB33_MAP.get(theObject
							.getCjsf()));
					theObject.setPcdw((String) Constants.G099_MAP.get(theObject
							.getPcdw()));
					theObject.setCgjfly((String) Constants.ZB32_MAP.get(theObject
							.getCgjfly()));

				}
				session.setAttribute(Constants.CGJXX_LIST, list);
			}
		}
		return mapping.findForward("list");
	}
}
